﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;


//n=1
//for collision, have ratarray[m] cehck for colision with ratarray[m+n]
    //n++
//m++
namespace Batty
{
    public class RatOld
    {
        private Pondhawk game;
        private Model model;
        private int frontleft;          // Index to the wing 1 bone
        private int frontright;          // Index to the wing 2 bone
        private int backleft;          // Index to the wing 1 bone
        private int backright;          // Index to the wing 2 bone
        private float legangle1 = 0f; // Current wing deployment angle
        private float legangle2 = -.8f; // Current wing deployment angle
        private int legState = 1;
        private bool confused = false;

        /// <summary>
        /// How fast we are going (cm/sec)
        /// </summary>
        private float speed = 2; 
        /// <summary>
        /// Current position
        /// </summary>
        private Vector3 position = Vector3.Zero;

        /// <summary>
        /// Compass heading (radians, 0 is Z direction)
        /// </summary>
        private float azimuth = 0;

        //Constructor
           public RatOld(Pondhawk game)
        {
            this.game = game;
        }
         /// <summary>
        /// This function is called to load content into this component
        /// of our game.
        /// </summary>
        /// <param name="content">The content manager to load from.</param>
        public void LoadContent(ContentManager content)
        {
            model = content.Load<Model>("Rat-rigid");
            frontleft = model.Bones.IndexOf(model.Bones["LeftFrontLeg"]);
            frontright = model.Bones.IndexOf(model.Bones["RightFrontLeg"]);
            backleft = model.Bones.IndexOf(model.Bones["LeftBackLeg"]);
            backright = model.Bones.IndexOf(model.Bones["RightBackLeg"]);
        }

        /// <summary>
        /// This function is called to update this component of our game
        /// to the current game time.
        /// </summary>
        /// <param name="gameTime"></param>
        public void Update(GameTime gameTime)
        {
            float legMoveTime = .7f;
            if (legState==0)
                legangle1 += (float)(0.30 * gameTime.ElapsedGameTime.TotalSeconds / legMoveTime);
            else
                legangle1 -= (float)(0.30 * gameTime.ElapsedGameTime.TotalSeconds / legMoveTime);
            if (legangle1 >= 0.30f)
            {
                legangle1 = 0.30f;
                legState = 1;
            }
            if (legangle1 <= -0.30f)
            {
                legangle1 = -0.30f;
                legState = 0;
            }
            Matrix transform = Matrix.CreateRotationY(azimuth);
            Vector3 directedThrust = Vector3.TransformNormal(new Vector3(0, 0, 1), transform);
            position +=  directedThrust* speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
            System.Diagnostics.Trace.WriteLine(legangle1.ToString());
        }

        /// <summary>
        /// This function is called to draw this game component.
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="gameTime"></param>
        public void Draw(GraphicsDeviceManager graphics, GameTime gameTime)
        {
            Matrix transform = Matrix.CreateRotationY(azimuth) * Matrix.CreateTranslation(position);

            DrawModel(graphics, model, transform);
        }
        private void DrawModel(GraphicsDeviceManager graphics, Model model, Matrix world)
        {
            Matrix[] transforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(transforms);
            transforms[frontleft] = Matrix.CreateRotationX(legangle1) * transforms[frontleft];
            transforms[frontright] = Matrix.CreateRotationX(-legangle1) * transforms[frontright];
            transforms[backleft] = Matrix.CreateRotationX(-legangle1) * transforms[backleft];
            transforms[backright] = Matrix.CreateRotationX(legangle1) * transforms[backright];
        





            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.World = transforms[mesh.ParentBone.Index] * world;
                    effect.View = Matrix.CreateLookAt(new Vector3(20, 20, 20),
                                                      new Vector3(0, 0, 0),
                                                      new Vector3(0, 1, 0));
                    effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(35),
                        graphics.GraphicsDevice.Viewport.AspectRatio, 10, 10000);
                }
                mesh.Draw();
            }
        }

        public void Confused() 
        { 
               //make the rat rotate directions
        }

        public bool ConfusedState { get { return confused; } set { confused = value; } }
        public Vector3 Position { get { return position; } set { position = value; } }

    }
}
